﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PropertyTools.Wpf;
using InteControlSys.ViewModel;

namespace InteControlSys
{
    public partial class DelegateTreeView  : UserControl
    {
        //DsControlExViewModel dsVM;
        public DelegateTreeView()
        {
            InitializeComponent();
            //DataContext = new MainViewModel();
            //tree1.KeyDown += this.tree1_KeyDown;
            tree1.SelectionChanged += new SelectionChangedEventHandler(tree1_SelectionChanged);
            //this.DataContextChanged +=new DependencyPropertyChangedEventHandler(ConfigResourceTreeView_DataContextChanged);
           // tree1.ch

            this.Loaded += new RoutedEventHandler(Tree1View_Loaded);

        }
        //void ConfigResourceTreeView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        //{
        //    //this.UpdateLayout();
        //}

        //BindingExpression binding;
        void Tree1View_Loaded(object sender, RoutedEventArgs e)
        {
            //if (dsVM == null)
            //{
            //    dsVM = (this.DataContext as ConfigResourceTreeViewModel).Parent;
            //}
            
            //this.DataContext = dsVM.ConfigRTreeVM;

            //binding = this.tree1.GetBindingExpression(TreeListBox.TreeSourceProperty);
        }
        
        public void ScrollIntoView(object ob)
        {
            tree1.ScrollIntoView(ob);
            //tree1.SelectedItem = tree1.Items[tree1.Items.Count - 1];
            ScrollViewer sv = new ScrollViewer();
            //tree1.
        }
        
        void tree1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.F2:
                    var cvm = ((TreeListBox)sender).SelectedItem as NodeViewModel;
                    if (cvm != null) cvm.IsEditing = true;
                    break;
                case Key.Delete:
                    //Delete(null, null);
                    break;
            }
        }

        public void tree1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            ListBox lb = sender as ListBox;
            if (lb.SelectedItem != null)
            {
                ((DelegateViewModel)this.DataContext).SelectedNode = lb.SelectedItem as NodeViewModel;

                tree1.ScrollIntoView(lb.SelectedItem);
            }
            else
            {
                ((ProcessViewModel)this.DataContext).SelectedNode = null;               
            }

            MainWindowViewModel parent = ((DelegateViewModel)this.DataContext).Parent;
            parent.CurrentSelectedObject = ((DelegateViewModel)this.DataContext).SelectedNode;
        }
    }
}
